const Service = require('egg').Service;

class BankService extends Service {
    /**
     * 多个参数模糊查询
     * @param {*} params 
     * @returns 
     */
  async find(params) {
    const { ctx } = this;
    // 构建查询条件
    let sql = `select bank_id AS bid, 
        bank_name AS bname, 
        bank_type AS btype, img_url as logo from bank
        LEFT JOIN banklogo on bank.bank_type = banklogo.type where 1=1`;
    const values = [];
    let index = 0;
    while (index < params.length) {
        sql += ' and bank_name like ?';
        values.push('%' + params[index] + '%');
        index++;
    }

    const bank = await ctx.db.query(
      sql,
      values
    );
    return bank;
  }

  /**
   * 单个参数精准查询
   * @param {*} param 
   * @returns 
   */
  async findOne(param) {
    const { ctx } = this;
    // 构建查询条件
    const sql = `select bank_id AS bid, 
        bank_name AS bname, 
        bank_type AS btype, img_url as logo from bank
        LEFT JOIN banklogo on bank.bank_type = banklogo.type where bank_name = ?`;
    const result = await ctx.db.query(
      sql,
      param
    );
    return result;
  }
}

module.exports = BankService;
